package alg20250316;

public class Text3 {
    public int maxProduct(int[] nums) {
        int n =nums.length;
        //1 创建表
        int[] f = new int[n + 1];
        int[] g = new int[n + 1];
        //2 初始化
        f[0] = 1;
        g[0] = 1;
        //3 填表
        for (int i = 1; i <= n; i++) {
            if(nums[i - 1] > 0){
                f[i] = f[i - 1] * nums[i - 1];
                g[i] = g[i - 1] * nums[i - 1];
            }else{
                f[i] = g[i - 1] * nums[i - 1];
                g[i] = f[i - 1] * nums[i - 1];
            }
            f[i] = Math.max(nums[i - 1], f[i]);
            g[i] = Math.min(nums[i - 1], g[i]);
        }
        int max = f[1];
        for (int i = 2; i <= n; i++) {
            if(f[i] >max) max = f[i];
        }
        //4 返回值
        return max;
    }
}
